package cn.maihe.elg.operation.supports.electronicseal.anne.service.impl;

import cn.maihe.elg.operation.config.AnneSignConfig;
import cn.maihe.elg.operation.config.ElgConfig;
import cn.maihe.elg.operation.supports.electronicseal.anne.AnneServiceName;
import cn.maihe.elg.operation.supports.electronicseal.anne.BaseAnneSignService;
import cn.maihe.elg.operation.supports.electronicseal.anne.bean.annesign.contract.ContractDownloadRequest;
import cn.maihe.elg.operation.supports.electronicseal.anne.bean.annesign.contract.ContractDownloadRequestBody;
import cn.maihe.elg.operation.supports.electronicseal.anne.bean.annesign.contract.ContractDownloadResponse;
import cn.maihe.elg.operation.supports.electronicseal.anne.utils.annesign.ApiException;
import cn.maihe.elg.operation.supports.system.ElgResourceService;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;

/**
 * @Description 合同自动签署
 * @Author WJH
 * @Date 2021/08/13
 */
@Slf4j
@Service
public class ContractDownloadService extends BaseAnneSignService<ContractDownloadRequest, ContractDownloadResponse> {

    @Resource
    private ElgResourceService elgResourceService;

    public ContractDownloadService(AnneSignConfig anneSignConfig, ElgConfig elgConfig) {
        super(anneSignConfig, elgConfig);
    }

    @Override
    protected AnneServiceName serviceName() {
        return AnneServiceName.contract_download;
    }


    /**
     * 合同下载
     *
     * @param anneSignContractId
     * @return
     */
    public Path downloadContract(Long anneSignContractId) {

        ContractDownloadRequest request = this.buildBaseReq();
        ContractDownloadRequestBody body = new ContractDownloadRequestBody();
        body.setContractIdList(Lists.newArrayList(anneSignContractId));

        request.setBody(body);

        ContractDownloadResponse response = null;
        try {
            response = this.doPost(request);
        } catch (Exception e) {
            log.error("合同下载失败:{}", e.getMessage(), e);
            throw new ApiException("合同下载失败:" + e.getMessage(), e);
        }

        try {

            Path tmpPath = Files.createTempFile("", response.getBody().getName());
            return Files.write(tmpPath, Base64.getDecoder().decode(response.getBody().getBase64()));
        } catch (IOException e) {
            log.error("合同保存失败:{}", e.getMessage(), e);
            throw new ApiException("合同保存失败:" + e.getMessage(), e);
        }
    }


    /**
     * 合同下载
     *
     * @param anneSignContractId
     * @return
     */
    public Path downloadContract(Long anneSignContractId,String acceptOrderNo) {

        ContractDownloadRequest request = this.buildBaseReq();
        ContractDownloadRequestBody body = new ContractDownloadRequestBody();
        body.setContractIdList(Lists.newArrayList(anneSignContractId));

        request.setBody(body);

        ContractDownloadResponse response = null;
        try {
            response = this.doPost(request);
        } catch (Exception e) {
            log.error("合同下载失败:{}", e.getMessage(), e);
            throw new ApiException("合同下载失败:" + e.getMessage(), e);
        }

        try {
            String fileName = response.getBody().getName();
            if(response.getBody().getName().endsWith(".pdf")){
                fileName = fileName.replace(".pdf","");
            }
            Path tmpPath = elgResourceService.buildSaveAbsolutePath("tmp",acceptOrderNo, fileName,"pdf");//Files.createTempFile("", response.getBody().getName());
            return Files.write(tmpPath, Base64.getDecoder().decode(response.getBody().getBase64()));
        } catch (IOException e) {
            log.error("合同保存失败:{}", e.getMessage(), e);
            throw new ApiException("合同保存失败:" + e.getMessage(), e);
        }
    }

}
